package com.wjyf.reducing_bento.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.annotation.AuthCheck;
import com.wjyf.reducing_bento.common.BaseResponse;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.common.ResultUtils;
import com.wjyf.reducing_bento.model.dto.CommodityAddDto;
import com.wjyf.reducing_bento.model.dto.CommodityUpdateDto;
import com.wjyf.reducing_bento.model.entity.CommodityBean;
import com.wjyf.reducing_bento.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.wjyf.reducing_bento.constant.UserConstant.ADMIN;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:32
 */
@RestController
@RequestMapping("/commodity-bean")
@Api(tags = "商品")
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    @GetMapping("/get")
    @ApiOperation("获取商品分页")
    public BaseResponse get(
            @RequestParam @ApiParam("当前页号") Long current,
            @RequestParam @ApiParam("页面大小") Long pageSize
    ) {
        Page<CommodityBean> page = commodityService.getCommodityPage(current, pageSize);
        return ResultUtils.success(page);
    }

    @PostMapping("add")
    @ApiOperation("新增商品")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse add(@RequestBody CommodityAddDto commodityAddDto) {
        boolean isSuccess = commodityService.add(commodityAddDto);
        return isSuccess ? ResultUtils.success(isSuccess) : ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @PutMapping("update")
    @ApiOperation("修改商品")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse update(@RequestBody CommodityUpdateDto CommodityUpdateDto) {
        boolean isSuccess = commodityService.update(CommodityUpdateDto);
        return isSuccess ? ResultUtils.success(isSuccess) : ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping
    @ApiOperation("删除商品")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse delete(@RequestParam @ApiParam("商品id") Integer codId) {
        if (codId == null || codId <= 0) return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        boolean remove = commodityService.removeById(codId);
        return remove ? ResultUtils.success(remove) : ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }
}
